//
//  HELOrderListCell.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/8.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELOrderListCell.h"
#import "NSString+Utilities.h"
#import "BFPaperButton.h"
#import "UIImageView+BTimageView.h"
@implementation HELOrderListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)setModel:(HELOrderModel *)model{
  
    [self.btnBgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UIButton class]]) {
            [obj removeFromSuperview];
        }
    }];
    
    if (kScreenWidth == 320 && kScreenHeight ==480) {//iphone4
        self.dateL.text = [NSString timeStringYYMMDDFrom:[model.createTime longLongValue]];
   
    }else if(kScreenWidth == 320 && kScreenHeight ==568){ //iphone 5
        self.dateL.text = [NSString timeStringYYMMDDFrom:[model.createTime longLongValue]];
     
    }else if(kScreenWidth == 375 && kScreenHeight == 667){ //iphone 6
        self.dateL.text = [NSString timeStringYYMMDDFrom:[model.createTime longLongValue]];
        
    }else if(kScreenWidth == 414 && kScreenHeight == 736){ //iphone 6P
        self.dateL.text = [NSString timeStringYYMMDDFrom:[model.createTime longLongValue]];
       
    }
    
    [self.orderImg nc_setImageWithUrl:model.courseImage];
    self.orderNumL.text =[NSString stringWithFormat:@"订单编号:%@",model.id];
    
    self.markPriceL.text = [NSString stringWithFormat:@"¥%.2f",[model.amountDue doubleValue]];
     self.realPayPriceL.text = [NSString stringWithFormat:@"¥%.2f",[model.amountPay doubleValue]];
    self.courseNameL.text = model.courseName;
    self.statusL.text = model.statusDesc;
    
    
    if ([model.status containsString:@"pending_payment"]) {
       
         [self addBtnInBgWithtitle:@"去支付" frameType:0 withTag:0] ;
         [self addBtnInBgWithtitle:@"取消订单" frameType:1 withTag:1];
        
    }else if([model.status containsString:@"already_paid"]){
        if ([model.isInvoiced integerValue] == 0) {
            [self addBtnInBgWithtitle:@"申请开票" frameType:0 withTag:2];
        }else{
            [self addBtnInBgWithtitle:@"查看发票" frameType:0 withTag:3];
        }
    
    }else if ([model.status containsString:@"canceled"]){
    
        
    }
    

}

- (void)addBtnInBgWithtitle:(NSString *)title frameType:(NSInteger)frameType withTag:(NSInteger)tag{
   
 
    BFPaperButton *btn =[BFPaperButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 400+tag;
    btn.isRaised = YES;
    [btn setBackgroundColor:kPinchColor];
    [self.btnBgView addSubview:btn];
    btn.titleFont = [UIFont systemFontOfSize:15];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    CGFloat x = 0;
   
    
    CGFloat w = (kScreenWidth - 16) * .24;
    CGFloat space = 8;
    CGFloat h = 33;
    CGFloat y = 6;
    if (frameType==0) {
        x = (kScreenWidth - 16) - w - space;
    }else{
        x = (kScreenWidth - 16) - (w + space)*2;
    }
    btn.frame = CGRectMake(x, y, w, h);
    
    
}
- (void)btnAction:(UIButton *)btn{
    
    
    switch (btn.tag - 400) {
        case 0:
            if (self.payOrder) {
                self.payOrder();
            }
            break;
        case 1:
            if (self.cancelOrder) {
                self.cancelOrder();
            }
            break;
        case 2:
            if (self.applyFP) {
                self.applyFP();
            }
            break;
        case 3:
            if (self.openFp) {
                self.openFp();
            }
            break;
            
        default:
            break;
    }
    

}
@end
